//
//  juxin_encrypt.cpp
//  JuxinXxtea
//
//  Created by Wizard on 2017/6/5.
//
//

#include <memory.h>
#include "juxin_encrypt.h"
#include "xxtea.h"
#include "base64.h"

#define MD5_KEY ""
//#define XXTEA_KEY "603f554c53b493770a70c62fa811593a"
#define XXTEA_KEY "78sk89w8dh09sll892oa9n8hs9s892ja"

const char* GetEncryptString(const char* data) {
//  char *jstr = (char *) (*env)->GetStringUTFChars(env, str, JNI_FALSE);
  if (data == nullptr) {
    return "";
  }
  
  size_t ori_len = strlen(data);
  if (ori_len <=0) {
    return "";
  }
  
  size_t len;
  unsigned char *encrypt_data = (unsigned char *)xxtea_encrypt(data, ori_len, XXTEA_KEY, &len);
  if (encrypt_data == NULL) {
    return "";
  }
  char *base64_data = base64_encode(encrypt_data, len);
  SAFE_FREE(encrypt_data);
//  free(jstr);
  return base64_data;
}

const char* GetDecryptString(const char* data) {
  if (data == nullptr) {
    return "";
  }
  
  size_t ori_len = strlen(data);
  if (ori_len <=0) {
    return "";
  }
  
  size_t len, len2;
  unsigned char *base64_data = base64_decode(data, &len);
  if (base64_data == NULL) {
    return "";
  }
  char *decrypt_data = (char *)xxtea_decrypt(base64_data, len, XXTEA_KEY, &len2);
  SAFE_FREE(base64_data);
  
  return decrypt_data;
}

//std::string GetMD5Encode(std::string data) {
//  
//}
//std::string GetMD5Decode(std::string data) {
//  
//}
